package com.xiaozhengkeji.springcloud.ezcloud.api;


import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.xiaozhengkeji.springcloud.ezcloud.common.core.base.entitys.user.SystemUserEntity;
import com.xiaozhengkeji.springcloud.ezcloud.common.core.bean.RetResult;
import com.xiaozhengkeji.springcloud.ezcloud.common.core.feign.UserServiceFeign;
import com.xiaozhengkeji.springcloud.ezcloud.common.core.model.CodeEnum;
import com.xiaozhengkeji.springcloud.ezcloud.common.core.utils.EZJsonUtils;
import com.xiaozhengkeji.springcloud.ezcloud.common.core.utils.UserUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * OAuth Api
 */
@Slf4j
@RestController
@RequestMapping("/oauth_v1")
public class OAuth2API {

    @Resource
    UserServiceFeign userServiceFeign;

    /**
     * 用户登录
     */
    @ApiOperation("用户登录")
    @PostMapping("/login/{appID}")
    public RetResult login(@ApiParam("登录App") @PathVariable("appID") String appID,
                           @ApiParam("邮箱") @RequestParam("mailbox") String mailbox,
                           @ApiParam("密码") @RequestParam("password") String password) {
        List<SystemUserEntity> systemUserEntities = userServiceFeign.infoByMailbox(mailbox, false);
        if (systemUserEntities.size() < 1) {
            return new RetResult(CodeEnum.NOT_FOUND.getCode(), "邮箱不存在!", null);
        }
        SystemUserEntity systemUser = systemUserEntities.get(0);
        if (!systemUser.getPassword().equals(UserUtils.userPasswordHash(password))) {
            return new RetResult(CodeEnum.UNAUTHORIZED.getCode(), "用户密码错误!", null);
        }
        StpUtil.login(systemUser.getUid(), appID);
        return new RetResult(CodeEnum.SUCCESS.getCode(), "登录成功", StpUtil.getTokenInfo());
    }

    /**用户注册*/

    /**用户信息*/

    /**退出登录*/


}
